package com.xjcs.saplingplan.ui.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.jeremyliao.liveeventbus.LiveEventBus
import com.xjcs.saplingplan.const.EventBusConstants
import com.xjcs.saplingplan.room.bean.countdown.CountdownBean
import com.xjcs.saplingplan.room.bean.countdown.PlanCateBean
import com.xjcs.saplingplan.room.utils.PlanCateRoomUtils
import com.xjcs.saplingplan.room.utils.CountdownRoomUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CountdownViewModel(application: Application) : AndroidViewModel(application) {

    val countdownList = MutableLiveData<MutableList<CountdownBean>>()
    val countdownCateList = MutableLiveData<MutableList<PlanCateBean>>()

    /**
     * 插入倒数日数据
     */
    fun insertCountdownBean(countDownBean: CountdownBean) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                CountdownRoomUtils.insert(countDownBean)
                withContext(Dispatchers.Main){
                    LiveEventBus.get<String>(EventBusConstants.REFRESH_COUNTDOWN_LIST)
                }
            } catch (_: Exception) {
            }
        }
    }

    /**
     * 获取倒数日数据
     */
    fun getCountdownList(userId: String, cateName: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val dataList = CountdownRoomUtils.getAllCountdownList(userId, cateName)
            withContext(Dispatchers.Main) {
                countdownList.value = dataList
            }
        }
    }

    /**
     * 获取倒数日分类数据
     */
    fun getCountdownCateList(userId: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val cateList = PlanCateRoomUtils.getAllPlanCateList(userId)
            withContext(Dispatchers.Main) {
                if (cateList.isEmpty()) {
                    countdownCateList.value = getDefaultCateList(userId)
                } else {
                    countdownCateList.value = cateList
                }
            }
        }
    }

    /**
     * 默认分类
     */
    private fun getDefaultCateList(userId: String): MutableList<PlanCateBean> {
        val list = mutableListOf<PlanCateBean>().apply {
            add(PlanCateBean().apply {
                planCateName = ""
                planCateUserId = userId
                planCateIcon = ""
                planCateSort = 100
            })
            add(PlanCateBean().apply {
                planCateName = "纪念日"
                planCateUserId = userId
                planCateIcon = ""
                planCateSort = 200
            })
            add(PlanCateBean().apply {
                planCateName = "节日"
                planCateUserId = userId
                planCateIcon = ""
                planCateSort = 300
            })
            add(PlanCateBean().apply {
                planCateName = "生日"
                planCateUserId = userId
                planCateIcon = ""
                planCateSort = 400
            })
        }
        PlanCateRoomUtils.insertList(list)
        return list
    }

}